\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<load data>>=
load("/Users/hannahridge/Downloads/cses_imd 2.rdata")
#load cses_imd 2.rdata from CSES
cses_imd <- subset(cses_imd, cses_imd$IMD1004!="HKG_1998" & cses_imd$IMD1004!="HKG_2000" & cses_imd$IMD1004!="HKG_2004" & cses_imd$IMD1004!="HKG_2008" & cses_imd$IMD1004!="HKG_2012" & cses_imd$IMD1004!="BLR_2001" & cses_imd$IMD1004!="BLR_2008" & cses_imd$IMD1004!="HKG_2012" & cses_imd$IMD1004!="TWN_1996" & cses_imd$IMD1004!="TWN_2001" & cses_imd$IMD1004!="TWN_2004" & cses_imd$IMD1004!="TWN_2008" & cses_imd$IMD1004!="TWN_2012" & cses_imd$IMD1004!="RUS_1999" & cses_imd$IMD1004!="RUS_2000" & cses_imd$IMD1004!="RUS_2004" & cses_imd$IMD1004!="KGZ_2005" & cses_imd$IMD1004!="PER_2000" & cses_imd$IMD1004!="THA_2007" & cses_imd$IMD1004!="THA_2011" & cses_imd$IMD1004!="TUR_2015" & cses_imd$IMD1004!="MEX_1997" & cses_imd$IMD1004!="UKR_1998") 
#drop all polity lower than 8, drop HKG, TWN
@

<<remove dks>>=
### Getting rid of the Refused, Don't Know, and Missing responses in the age variable
cses_imd <- subset(cses_imd, cses_imd$IMD2001_1  < 9997)

### Getting rid of the Refused and Missing responses in the Gender variable
cses_imd <- subset(cses_imd, cses_imd$IMD2002 <= 2)

### Getting rid of [see election study notes] and Refused, Don't Know, and Missing responses to the Education variable
cses_imd <- subset(cses_imd, cses_imd$IMD2003 < 6)

### Removing Refused, Don't Know, Missing, and [See election study notes] responses from the SWD variable
cses_imd <- subset(cses_imd, cses_imd$IMD3010 < 7)
cses_imd$IMD3010 <- recode(cses_imd$IMD3010, "5=1;4=2;6=3;2=4;1=5")

### Turning Gender variable into a factor
cses_imd$IMD2002 <- as.factor(cses_imd$IMD2002)

##make a close to party binary
cses_imd$closeparty <- ifelse(cses_imd$IMD3005_1 == 1, 1, 0) #binary instead of scale since there is substantial missingness for the second question

##make a time variable.  years since 2001, the first year in the dataset
cses_imd$time <- cses_imd$IMD1008_YEAR - 1996
@

<<merge german files>>=
### Recoding two German samples into one
cses_imd$IMD1004[cses_imd$IMD1004=="DEU12002"] <- "DEU_2002"
cses_imd$IMD1004[cses_imd$IMD1004=="DEU22002"] <- "DEU_2002"
@

<<gdp per capita>>=
### add GDP per capita for year before, PPP (constant 2011 international $) from World Bank (NY.GDP.PCAP.PP.KD)
cses_imd$gdp <- NA
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "ALB_2005", 7000.527022, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "AUS_1996", 33791.80147, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "AUS_2004", 37183.68457, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "AUS_2007", 39445.13292, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "AUS_2013", 42771.40125, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "AUT_2008", 43939.42529, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "AUT_2013", 44551.61757, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "BELF1999", 34813.86094, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "BELW1999", 34813.86094, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "BEL_2003", 37856.86339, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "BGR_2001", 8833.439772, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "BGR_2014", 15938.93884, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "BRA_2002", 11369.00712, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "BRA_2006", 12299.3795, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "BRA_2010", 13653.01398, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "BRA_2014", 15430.26577, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CAN_1997", 32394.57787, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CAN_2004", 38805.67111, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CAN_2008", 41647.38781, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CAN_2011", 40699.35513, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CAN_2015", 43079.14252, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CHE_1999", 48540.27293, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CHE_2003", 50815.15316, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CHE_2007", 54531.9937, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CHE_2011", 55866.30507, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CHL_2005", 16258.38895, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CZE_1996", 19270.69939, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CZE_1999", 19962.96129, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CZE_2002", 21892.48253, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CZE_2006", 25781.20471, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CZE_2010", 27803.58716, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "CZE_2013", 28527.13873, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "DEU_1998", 34405.51921, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "DEU_2002", 37325.05255, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "DEU_2005", 37418.0925, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "DEU_2009", 40989.44311, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "DEU_2013", 42822.0994, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "DNK_1998", 39180.68656, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "DNK_2001", 42337.71437, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "DNK_2007", 46159.91374, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "ESP_1996", 25051.23368, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "ESP_2000", 28589.30859, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "ESP_2004", 31815.42134, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "ESP_2008", 34329.647, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "EST_2011", 22740.972, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "FIN_2003", 36218.24016, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "FIN_2007", 40546.08073, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "FIN_2011", 39848.1345, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "FIN_2015", 39017.53721, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "FRA_2002", 35265.52948, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "FRA_2007", 37071.97413, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "FRA_2012", 37440.64149, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "GBR_1997", 29309.2894, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "GBR_2005", 36259.51341, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "GBR_2015", 38418.58645, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "GRC_2009", 31881.71235, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "GRC_2012", 26141.31959, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == "GRC_2015", 24081.63054, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'HRV_2007', 21076.47894, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'HUN_1998', 15893.08341, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'HUN_2002', 18684.76468, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'IRL_2002', 39948.63522, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'IRL_2007', 47279.38224, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'IRL_2011', 43514.76049, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ISL_1999', 32197.78676, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ISL_2003', 34852.18459, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ISL_2007', 40770.17888, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ISL_2009', 43406.38481, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ISL_2013', 41076.60612, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ISR_1996', 23795.25212, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ISR_2003', 25644.89556, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ISR_2006', 26677.41467, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ISR_2013', 30644.85235, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ITA_2006', 37604.36062, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'JPN_1996', 32425.22995, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'JPN_2004', 34333.13367, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'JPN_2007', 36141.58524, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'JPN_2013', 36367.59706, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'KEN_2013', 2602.50409, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'KGZ_2005', 2370.166045, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'KOR_2000', 19216.14147, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'KOR_2004', 23549.36645, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'KOR_2008', 28013.70442, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'KOR_2012', 31228.5107, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'LTU_1997', 9914.353838, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'LVA_2010', 18609.29859, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'LVA_2011', 18251.91969, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'LVA_2014', 21563.68174, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'MEX_1997', 13759.33146, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'MEX_2000', 15155.63913, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'MEX_2003', 15218.79392, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'MEX_2006', 15791.53754, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'MEX_2009', 16293.03182, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'MEX_2012', 16049.36476, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'MEX_2015', 16615.97998, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'MNE_2012', 14472.461, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NLD_1998', 37205.27913, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NLD_2002', 42309.41715, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NLD_2006', 43529.78608, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NLD_2010', 45149.12806, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NOR_1997', 52302.69134, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NOR_2001', 58045.06727, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NOR_2005', 61675.60078, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NOR_2009', 64586.1779, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NOR_2013', 63003.4109, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NZL_1996', 25052.35446, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NZL_2002', 28500.95264, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NZL_2008', 32657.58008, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NZL_2011', 32119.21554, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'NZL_2014', 33782.10082, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PER_2006', 7595.271846, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PER_2011', 9956.556637, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PER_2016', 11770.03445, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PHL_2004', 4441.675324, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PHL_2010', 5284.865261, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PHL_2016', 6874.582566, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'POL_1997', 11975.5672, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'POL_2001', 14732.482, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'POL_2005', 16605.81843, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'POL_2007', 18267.58802, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'POL_2011', 21770.64444, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PRT_2002', 26318.47816, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PRT_2005', 26439.93318, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PRT_2009', 27590.37495, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'PRT_2015', 26023.69785, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ROU_1996', 10443.16084, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ROU_2004', 12705.0714, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ROU_2009', 18832.92997, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ROU_2012', 17907.64749, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ROU_2014', 19077.18911, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SRB_2012', 12967.89199, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SVK_2010', 23973.84139, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SVK_2016', 28397.41072, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SVN_1996', 18431.04519, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SVN_2004', 24896.11909, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SVN_2008', 30190.72365, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SVN_2011', 28678.37263, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SWE_1998', 32393.12965, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SWE_2002', 37330.28658, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SWE_2006', 41226.84665, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'SWE_2014', 43522.35122, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'THA_2001', 9189.060781, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'THA_2007', 12024.67183, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'THA_2011', 13486.56241, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'TUR_2011', 17959.25635, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'TUR_2015', 22401.87088, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'UKR_1998', 4497.390275, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'URY_2009', 15307.85795, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'USA_1996', 39485.65953, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'USA_2004', 47271.28787, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'USA_2008', 51023.56363, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'USA_2012', 49883.11686, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ZAF_2009', 11989.86404, cses_imd$gdp)
cses_imd$gdp <- ifelse(cses_imd$IMD1004 == 'ZAF_2014', 12339.73468, cses_imd$gdp)
@

<<like>>=
cses_imd$like <- NA

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_1==1 & cses_imd$IMD1004=="CZE_1996" | cses_imd$IMD1004=="GBR_1997" | cses_imd$IMD1004=="DNK_1998" | cses_imd$IMD1004=="ISL_1999" | cses_imd$IMD1004=="ISR_1996" | cses_imd$IMD1004=="LTU_1997" | cses_imd$IMD1004=="MEX_1997" | cses_imd$IMD1004=="MEX_2000" | cses_imd$IMD1004=="NLD_1998" | cses_imd$IMD1004=="NZL_1996" |  cses_imd$IMD1004=="NOR_1997" | cses_imd$IMD1004=="POL_1997" | cses_imd$IMD1004=="PRT_2002" | cses_imd$IMD1004=="ROU_1996" | cses_imd$IMD1004=="SVN_1996" | cses_imd$IMD1004=="KOR_2000" | cses_imd$IMD1004=="ESP_1996" | cses_imd$IMD1004=="ESP_2000" | cses_imd$IMD1004=="CHE_1999" | cses_imd$IMD1004=="THA_2001" | cses_imd$IMD1004=="UKR_1998" | cses_imd$IMD1004=="USA_1996", cses_imd$IMD3008_A, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_1==1 & cses_imd$IMD1004=="AUS_1996" | cses_imd$IMD1004=="BELF1999" | cses_imd$IMD1004=="BELW1999" | cses_imd$IMD1004=="CAN_1997" | cses_imd$IMD1004=="DEU_1998" | cses_imd$IMD1004=="HUN_1998" | cses_imd$IMD1004=="JPN_1996" | cses_imd$IMD1004=="SWE_1998", cses_imd$IMD3008_B, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_2==1 & cses_imd$IMD1004=="ALB_2005" | cses_imd$IMD1004=="AUS_2004" | cses_imd$IMD1004=="BEL_2003" | cses_imd$IMD1004=="BRA_2002" | cses_imd$IMD1004=="BGR_2001" | cses_imd$IMD1004=="CAN_2004" | cses_imd$IMD1004=="CZE_2002" | cses_imd$IMD1004=="DNK_2001" |  cses_imd$IMD1004=="DEU_2002" | cses_imd$IMD1004=="FIN_2003" | cses_imd$IMD1004=="DEU2002" | cses_imd$IMD1004=="GBR_2005" | cses_imd$IMD1004=="HUN_2002" | cses_imd$IMD1004=="ISL_2003" | cses_imd$IMD1004=="ISR_2003" |  cses_imd$IMD1004=="IRL_2002" | cses_imd$IMD1004=="KGZ_2005" | cses_imd$IMD1004=="MEX_2003" | cses_imd$IMD1004=="NLD_2002" | cses_imd$IMD1004=="NZL_2002" | cses_imd$IMD1004=="PHL_2004" | cses_imd$IMD1004=="POL_2001" | cses_imd$IMD1004=="PRT_2005" | cses_imd$IMD1004=="ROU_2004" | cses_imd$IMD1004=="SVN_2004" | cses_imd$IMD1004=="ESP_2004" | cses_imd$IMD1004=="KOR_2004" | cses_imd$IMD1004=="SWE_2002" | cses_imd$IMD1004=="USA_2004", cses_imd$IMD3008_A, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_2==1 & cses_imd$IMD1004=="ITA_2006" | cses_imd$IMD1004=="JPN_2004" | cses_imd$IMD1004=="PER_2006" | cses_imd$IMD1004=="CHE_2003", cses_imd$IMD3008_B, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_2==1 & cses_imd$IMD1004=="FRA_2002" , cses_imd$IMD3008_H, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_2==1 & cses_imd$IMD1004=="NOR_2001" | cses_imd$IMD1004=="CHL_2005", cses_imd$IMD3008_E, cses_imd$like)
                          
cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_3==1 & cses_imd$IMD1004=="AUT_2008" | cses_imd$IMD1004=="BRA_2010" | cses_imd$IMD1004=="HRV_2007" | cses_imd$IMD1004=="CZE_2006" | cses_imd$IMD1004=="DNK_2007" | cses_imd$IMD1004=="EST_2011" |
cses_imd$IMD1004=="FIN_2011" | cses_imd$IMD1004=="FIN_2007" | cses_imd$IMD1004=="FRA_2007" | cses_imd$IMD1004=="DEU_2009" | cses_imd$IMD1004=="GRC_2009" | cses_imd$IMD1004=="ISL_2007" | cses_imd$IMD1004=="ISL_2009" | cses_imd$IMD1004=="IRL_2007" | cses_imd$IMD1004=="ISR_2006" | cses_imd$IMD1004=="LVA_2010" | cses_imd$IMD1004=="MEX_2006" | cses_imd$IMD1004=="NLD_2010" | cses_imd$IMD1004=="NLD_2006" | cses_imd$IMD1004=="NZL_2008" | cses_imd$IMD1004=="NOR_2009" | cses_imd$IMD1004=="PER_2011" | cses_imd$IMD1004=="PHL_2010" | cses_imd$IMD1004=="POL_2005" | cses_imd$IMD1004=="POL_2007" | cses_imd$IMD1004=="PRT_2009" | cses_imd$IMD1004=="ROU_2009" | cses_imd$IMD1004=="SVN_2008" | cses_imd$IMD1004=="ZAF_2009" | cses_imd$IMD1004=="KOR_2008" | cses_imd$IMD1004=="ESP_2008" | cses_imd$IMD1004=="CHE_2007" | cses_imd$IMD1004=="THA_2007" | cses_imd$IMD1004=="TUR_2011" | cses_imd$IMD1004=="URY_2009" | cses_imd$IMD1004=="USA_2008" | cses_imd$IMD1004=="NOR_2005", cses_imd$IMD3008_A, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_3==1 & cses_imd$IMD1004=="AUS_2007" | cses_imd$IMD1004=="BRA_2006" | cses_imd$IMD1004=="CAN_2008" | cses_imd$IMD1004=="CHL_2009" | cses_imd$IMD1004=="CZE_2010" | cses_imd$IMD1004=="DEU_2005" | cses_imd$IMD1004=="JPN_2007" | cses_imd$IMD1004=="MEX_2009" | cses_imd$IMD1004=="SVK_2010" | cses_imd$IMD1004=="SWE_2006", cses_imd$IMD3008_B, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_4==1 & cses_imd$IMD1004=="AUS_2013" | cses_imd$IMD1004=="AUT_2013" | cses_imd$IMD1004=="BRA_2014" | cses_imd$IMD1004=="BGR_2014" | cses_imd$IMD1004=="CAN_2011" | cses_imd$IMD1004=="CAN_2015" | cses_imd$IMD1004=="CZE_2013" | cses_imd$IMD1004=="FIN_2015" | cses_imd$IMD1004=="FRA_2012" | cses_imd$IMD1004=="DEU_2013" | cses_imd$IMD1004=="GRC_2012" |cses_imd$IMD1004=="GRC_2015" | cses_imd$IMD1004=="GBR_2015" |cses_imd$IMD1004=="IRL_2011" | cses_imd$IMD1004=="ISR_2013" | cses_imd$IMD1004=="JPN_2013" | cses_imd$IMD1004=="KEN_2013" | cses_imd$IMD1004=="MEX_2012" | cses_imd$IMD1004=="MEX_2015" | cses_imd$IMD1004=="MNE_2012" | cses_imd$IMD1004=="NZL_2011" |cses_imd$IMD1004=="NZL_2014" | cses_imd$IMD1004=="NOR_2013" | cses_imd$IMD1004=="POL_2011" | cses_imd$IMD1004=="SVK_2016" | cses_imd$IMD1004=="ZAF_2014" | cses_imd$IMD1004=="KOR_2012" | cses_imd$IMD1004=="SWE_2014" | cses_imd$IMD1004=="CHE_2011" | cses_imd$IMD1004=="THA_2011" | cses_imd$IMD1004=="TUR_2015" | cses_imd$IMD1004=="USA_2012", cses_imd$IMD3008_A, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_4==1 & cses_imd$IMD1004=="ISL_2013" | cses_imd$IMD1004=="LVA_2014" | cses_imd$IMD1004=="PER_2016" | cses_imd$IMD1004=="PRT_2015" | cses_imd$IMD1004=="ROU_2014" | cses_imd$IMD1004=="SVN_2011", cses_imd$IMD3008_B, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_4==1 & cses_imd$IMD1004=="LVA_2011" | cses_imd$IMD1004=="SRB_2012", cses_imd$IMD3008_C, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_4==1 & cses_imd$IMD1004=="ARG_2015" | cses_imd$IMD1004=="ROU_2012", cses_imd$IMD3008_G, cses_imd$like)

cses_imd$like <- ifelse(cses_imd$IMD1008_MOD_4==1 & cses_imd$IMD1004=="PHL_2016" , cses_imd$IMD3008_F, cses_imd$like)

cses_imd <- subset(cses_imd, cses_imd$like < 96)
@

<<like binary>>=
table(cses_imd$IMD3008_G)
cses_imd$dislike <- 999
cses_imd$dislike <- ifelse(cses_imd$IMD3008_A<5 | cses_imd$IMD3008_B<5 |cses_imd$IMD3008_C<5 |cses_imd$IMD3008_D<5 | cses_imd$IMD3008_E<5|cses_imd$IMD3008_F<5|cses_imd$IMD3008_G<5 |cses_imd$IMD3008_H<5|cses_imd$IMD3008_I<5, 1, 0)
@

<<models>>=
### Removing Refused, Don't Know, Missing, and [See election study notes] responses from the SWD variable

cses_imd$module <- 999
cses_imd$module <- ifelse(cses_imd$IMD1008_MOD_1 == 1, 1, cses_imd$module)
cses_imd$module <- ifelse(cses_imd$IMD1008_MOD_2 == 1, 2, cses_imd$module)
cses_imd$module <- ifelse(cses_imd$IMD1008_MOD_3 == 1, 3, cses_imd$module)
cses_imd$module <- ifelse(cses_imd$IMD1008_MOD_4 == 1, 4, cses_imd$module)
table(cses_imd$module)

cses_imd$IMD30102 <- recode(cses_imd$IMD3010, "3=NA; 4=3; 5=4")

cses_imdc <- subset(cses_imd, cses_imd$IMD2006<6) #income
cses_imdd <- subset(cses_imdc, cses_imdc$IMD3006<11) #income and ideology

start.time <- Sys.time()  #20 min
imd74 <- clmm(factor(IMD30102) ~ scale(IMD2001_1) + IMD2002 + scale(IMD2003) + scale(gdp) + closeparty + IMD2006 + like + factor(module) + (1 | IMD1004), data = cses_imdc, link="logit")
summary(imd74) #like pos, mods neg
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

start.time <- Sys.time()  #20.7 min
imd74bin <- clmm(factor(IMD30102) ~ scale(IMD2001_1) + IMD2002 + scale(IMD2003) + scale(gdp) + closeparty + IMD2006 + dislike + factor(module) + (1 | IMD1004), data = cses_imdc, link="logit")
summary(imd74bin) #dislike neg, mods neg
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

htmlreg(list(imd74, imd74bin))
@

<<>>=
mod1 <- subset(cses_imdd, cses_imdd$IMD1008_MOD_1 == 1)
mod2 <- subset(cses_imdd, cses_imdd$IMD1008_MOD_2 == 1)
mod3 <- subset(cses_imdd, cses_imdd$IMD1008_MOD_3 == 1)
mod4 <- subset(cses_imdd, cses_imdd$IMD1008_MOD_4 == 1)

start.time <- Sys.time()  #2.1 min
model1 <- clmm(factor(IMD30102) ~ scale(IMD2001_1) + IMD2002 + scale(IMD2003) + scale(gdp) + closeparty + IMD2006 + IMD3006 + time + like + (1 | IMD1004), data = mod1, link="logit")
summary(model1) 
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

start.time <- Sys.time()  #2.3 min
model2 <- clmm(factor(IMD30102) ~ scale(IMD2001_1) + IMD2002 + scale(IMD2003) + scale(gdp) + closeparty + IMD2006 + IMD3006 + time + like + (1 | IMD1004), data = mod2, link="logit")
summary(model2) 
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

start.time <- Sys.time()  #3.2  min
model3 <- clmm(factor(IMD30102) ~ scale(IMD2001_1) + IMD2002 + scale(IMD2003) + scale(gdp) + closeparty + IMD2006 + IMD3006 + time + like + (1 | IMD1004), data = mod3, link="logit")
summary(model3) 
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

start.time <- Sys.time()  #3.3  min
model4 <- clmm(factor(IMD30102) ~ scale(IMD2001_1) + IMD2002 + scale(IMD2003) + scale(gdp) + closeparty + IMD2006 + IMD3006 + time + like + (1 | IMD1004), data = mod4, link="logit")
summary(model4) 
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
@


\end{document}